Set a global default User-Agent value via an environment variable.

Names starting with HTTP_ should be avoided in the context of Web app,
hence the rather verbose name `DEFAULT_HTTP_USER_AGENT`.

Akinori MUSHA 10 years ago
parent
commit
7ecbe78dff
3 changed files with 37 additions and 5 deletions
  1. 4 0
      .env.example
  2. 7 4
      app/concerns/web_request_concern.rb
  3. 26 1
      spec/support/shared_examples/web_request_concern.rb

+ 4 - 0
.env.example

@@ -92,6 +92,10 @@ AWS_SANDBOX=false
92 92
 # require you to bundle a corresponding gem via Gemfile.
93 93
 FARADAY_HTTP_BACKEND=typhoeus
94 94
 
95
+# Specify the default User-Agent header value for HTTP requests made
96
+# by Agents that allow overriding the User-Agent header value.
97
+DEFAULT_HTTP_USER_AGENT="Huginn - https://github.com/cantino/huginn"
98
+
95 99
 # Allow JSONPath eval expresions. i.e., $..price[?(@ < 20)]
96 100
 # You should not allow this on a shared Huginn box because it is not secure.
97 101
 ALLOW_JSONPATH_EVAL=false

+ 7 - 4
app/concerns/web_request_concern.rb

@@ -21,9 +21,7 @@ module WebRequestConcern
21 21
     @faraday ||= Faraday.new { |builder|
22 22
       builder.headers = headers if headers.length > 0
23 23
 
24
-      if (user_agent = interpolated['user_agent']).present?
25
-        builder.headers[:user_agent] = user_agent
26
-      end
24
+      builder.headers[:user_agent] = user_agent
27 25
 
28 26
       builder.use FaradayMiddleware::FollowRedirects
29 27
       builder.request :url_encoded
@@ -58,4 +56,9 @@ module WebRequestConcern
58 56
   def faraday_backend
59 57
     ENV.fetch('FARADAY_HTTP_BACKEND', 'typhoeus').to_sym
60 58
   end
61
-end
59
+
60
+  def user_agent
61
+    interpolated['user_agent'].presence ||
62
+      ENV.fetch('DEFAULT_HTTP_USER_AGENT', Faraday.new.headers[:user_agent])
63
+  end
64
+end

+ 26 - 1
spec/support/shared_examples/web_request_concern.rb

@@ -63,4 +63,29 @@ shared_examples_for WebRequestConcern do
63 63
       agent.should_not be_valid
64 64
     end
65 65
   end
66
-end
66
+
67
+  describe "User-Agent" do
68
+    before do
69
+      @default_http_user_agent = ENV['DEFAULT_HTTP_USER_AGENT']
70
+      ENV['DEFAULT_HTTP_USER_AGENT'] = nil
71
+    end
72
+
73
+    after do
74
+      ENV['DEFAULT_HTTP_USER_AGENT'] = @default_http_user_agent
75
+    end
76
+
77
+    it "should have the default value set by Faraday" do
78
+      agent.user_agent.should == Faraday.new.headers[:user_agent]
79
+    end
80
+
81
+    it "should be overridden by the environment variable if present" do
82
+      ENV['DEFAULT_HTTP_USER_AGENT'] = 'Huginn - https://github.com/cantino/huginn'
83
+      agent.user_agent.should == 'Huginn - https://github.com/cantino/huginn'
84
+    end
85
+
86
+    it "should be overriden by the value in options if present" do
87
+      agent.options['user_agent'] = 'Override'
88
+      agent.user_agent.should == 'Override'
89
+    end
90
+  end
91
+end